<template>
	<div class="container">
		<!-- 路径导航区域 -->
		<el-breadcrumb separator="/">
			<el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
			<el-breadcrumb-item>系统管理</el-breadcrumb-item>
			<el-breadcrumb-item>普通用户管理</el-breadcrumb-item>
		</el-breadcrumb>
		<!-- 搜索区域 -->
		<el-row>
			<el-form :inline="true">
				<el-form-item label="用户名">
					<el-input v-model="queryInfo.userName" clearable @clear="queryList" placeholder="请输入用户名"></el-input>
				</el-form-item>
				<el-form-item label="昵称">
					<el-input v-model="queryInfo.userNickname" clearable @clear="queryList"
						placeholder="请输入昵称"></el-input>
				</el-form-item>
				<el-form-item label="手机号">
					<el-input v-model="queryInfo.userPhone" clearable @clear="queryList"
						placeholder="请输入手机号"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="queryList">搜索</el-button>
					<el-button type="primary" @click="toAddUser">新增普通用户</el-button>
				</el-form-item>
			</el-form>
		</el-row>
		<!-- 表格区域 -->
		<el-table :data="userList" border style="width: 100%">
			<el-table-column type="selection" width="55"></el-table-column>
			<el-table-column prop="userId" label="ID"></el-table-column>
			<el-table-column prop="userName" label="用户名">
			</el-table-column>
			<el-table-column prop="userNickname" label="昵称">
			</el-table-column>
			<el-table-column prop="userEmail" label="邮箱">
			</el-table-column>
			<el-table-column prop="userPhone" label="手机号">
			</el-table-column>
			<el-table-column prop="userAddress" label="地址">
			</el-table-column>
			<el-table-column prop="userPoint" label="积分">
			</el-table-column>
			<el-table-column label="操作">
				<template slot-scope='scope'>
					<el-link type="primary" @click="toEditUser(scope.row)">编辑</el-link>
					<el-link type="primary" @click="deleteUser(scope.row.userId)">删除</el-link>
				</template>
			</el-table-column>
		</el-table>
		<!-- 新增和修改的对话框 -->
		<el-dialog :title="title" :visible.sync="dialogFormVisible" @close="cancle">
			<el-form :model="user">
				<el-form-item label="用户名" label-width="80px">
					<el-input placeholder="请输入用户名" v-model="user.userName"></el-input>
				</el-form-item>
				<el-form-item label="密码" label-width="80px">
					<el-input placeholder="请输入密码" v-model="user.userPassword"></el-input>
				</el-form-item>
				<el-form-item label="手机号" label-width="80px">
					<el-input placeholder="请输入手机号" v-model="user.userPhone"> </el-input>
				</el-form-item>
				<el-form-item label="昵称" label-width="80px">
					<el-input placeholder="请输入昵称" v-model="user.userNickname"></el-input>
				</el-form-item>
				<el-form-item label="邮箱" label-width="80px">
					<el-input placeholder="请输入邮箱" v-model="user.userEmail"> </el-input>
				</el-form-item>
				<el-form-item label="地址" label-width="80px">
					<el-input placeholder="请输入地址" v-model="user.userAddress"> </el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="cancle">取 消</el-button>
				<el-button type="primary" @click="addUser">确 定</el-button>
			</div>
		</el-dialog>
		<!-- 分页区域 -->
		<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
			:current-page="queryInfo.pageNum" :page-sizes="[1,5, 10, 15, 20]" :page-size="queryInfo.pageSize"
			layout="total, sizes, prev, pager, next, jumper" :total="total">
		</el-pagination>
	</div>
</template>
<script>
	export default {
		data() {
			return {
				// 查询信息对象
				queryInfo: {
					userName: '',
					pageNum: 1,
					pageSize: 5
				},
				total: 183,
				userList: [],
				title: '', // 对话框标题
				dialogFormVisible: false, // 是否显示对话框
				user: { // 用户对象
					"userId": "",
					"userName": "",
					"userPassword": "",
					"userNickname": "",
					"userEmail": "",
					"userPhone": "",
					"userAddress": "",
					"userPoint": 100
				}
			}
		},
		methods: {
			// 删除用户
			deleteUser(id) {
				this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
						confirmButtonText: '确定',
						cancelButtonText: '取消',
						type: 'warning'
					})
					.then(() => {
						this.$http.delete('user/' + id)
							.then(resp => {
								// console.log(resp)
								if (resp.data.status == 200) {
									this.$message.success(resp.data.message)
									this.queryList()
								} else {
									this.$message.error(resp.data.message)
								}
							})
					})
					.catch(() => {
						this.$message({
							type: 'info',
							message: '已取消删除'
						})
					})
			},
			// 进入修改的预览数据页面
			toEditUser(row) {
				//console.log(row)
				// 赋值
				this.user = row;
				// 设置标题
				this.title = "修改用户";
				// 开启对话框
				this.dialogFormVisible = true;
			},
			/// 取消对话框
			cancle() {
				this.dialogFormVisible = false;
				this.user = { // 用户对象
						"userId": "",
						"userName": "",
						"userPassword": "",
						"userNickname": "",
						"userEmail": "",
						"userPhone": "",
						"userAddress": "",
						"userPoint": 100
					},
					this.queryList()
			},

			// 进入添加的对话框
			toAddUser() {
				this.title = "新增用户";
				this.dialogFormVisible = true;
			},
			// 添加用户
			addUser() {
				if (this.user.userId) { // 有值为true  修改
					this.$http.put("user", this.user)
						.then(res => {
							if (res.data.status == 200) {
								// 消息提示
								this.$message.success('修改成功!');
								// 清空数据
								this.user = { // 用户对象
									"userId": "",
									"userName": "",
									"userPassword": "",
									"userNickname": "",
									"userEmail": "",
									"userPhone": "",
									"userAddress": "",
									"userPoint": 100
								}
								// 关闭对话框
								this.dialogFormVisible = false;
								// 重新查询
								this.queryList()
							} else {
								this.$message.error('接口异常！')
							}
						})
				} else {
					this.$http.post("user", this.user)
						.then(res => {
							if (res.data.status == 200) {
								// 消息提示
								this.$message.success('添加成功!');
								// 清空数据
								this.user = { // 用户对象
									"userId": "",
									"userName": "",
									"userPassword": "",
									"userNickname": "",
									"userEmail": "",
									"userPhone": "",
									"userAddress": "",
									"userPoint": 100
								}
								// 关闭对话框
								this.dialogFormVisible = false;
								// 重新查询
								this.queryList()
							} else {
								this.$message.error('接口异常！')
							}
						})
				}
			},
			// 分页查询所有的数据
			queryList() {
				//调用后端接口
				this.$http.get('user/findByPage', {
						params: this.queryInfo
					})
					.then(res => {
						if (res.data.status == 200) {
							this.userList = res.data.data.list
							this.total = res.data.data.total
						} else {
							this.$message.error("查询失败")
						}
					})

			},
			handleSizeChange(size) {
				// console.log('每页显示数量发生改变');
				this.queryInfo.pageSize = size
				this.queryInfo.pageNum = 1
				this.queryList()
			},
			handleCurrentChange(num) {
				// console.log('页码发生改变');
				this.queryInfo.pageNum = num
				this.queryList()
			}
		},
		mounted() {
			this.queryList();
		}
	}
</script>
<style>
	.el-breadcrumb {
		margin-bottom: 15px;
		padding: 0 0 15px;
		border-bottom: 1px solid #ddd;
	}

	.el-pagination {
		margin-top: 15px;
	}

	.el-link {
		padding: 0 5px;
	}
</style>